package com.sinog.biz.util;

import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * @desc 数据库密码解密
 * @author lzq
 * @date -
 */
@Slf4j
public final class DesCryptoGraphy {

    /**
     * DES密钥 8位
     */
    public static final String KEY = "key01234";

    /**
     * 加密
     * @param content content
     * @param keyBytes keyBytes
     * @return byte[]
     */
    static byte[] desCbcEncrypt(byte[] content,byte[] keyBytes) {
        try {
            DESKeySpec keySpec = new DESKeySpec(keyBytes);
            //指定使什么样的算法
            String algorithm = "DES";
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            SecretKey key = keyFactory.generateSecret(keySpec);
            //用什么样的转型方式
            String transformation = "DES/CBC/PKCS5Padding";
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.ENCRYPT_MODE,key,new IvParameterSpec(keySpec.getKey()));
            return cipher.doFinal(content);
        } catch(InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
            log.error("发生异常:",e);
        }
        return null;
    }

    /**
     * 解密
     * @param content content
     * @param keyBytes keyBytes
     * @return byte[]
     */
    public static byte[] desCbcDecrypt(byte[] content,byte[] keyBytes) {
        try {
            DESKeySpec keySpec = new DESKeySpec(keyBytes);
            String algorithm = "DES";
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            SecretKey key = keyFactory.generateSecret(keySpec);
            String transformation = "DES/CBC/PKCS5Padding";
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE,key,new IvParameterSpec(keyBytes));
            return cipher.doFinal(content);
        } catch(InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
            log.error("发生异常:",e);
        }
        return null;
    }

    /**
     * 二进制转16进制
     * @param bytes bytes
     * @return String
     */
    static String byteToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(16);
        String sTemp;
        for(byte aByte : bytes) {
            sTemp = Integer.toHexString(0xFF & aByte);
            if(2 > sTemp.length()) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 16进制转二进制
     * @param s s
     * @return byte[]
     */
    public static byte[] hexStringToByte(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        try {
            for(int i = 0; i < len; i += Constants.Number.NUM_TWO) {
                data[i / 2] = (byte)((Character.digit(s.charAt(i),16) << 4) + Character.digit(s.charAt(i + 1),16));
            }
        } catch(RuntimeException e) {
            log.error("发生异常:",e);
        }
        return data;
    }
}